package com.bestvike.stone.spring.aop;

import com.bestvike.stone.spring.shiro.aop.AnnotationsAuthorizingMethodInterceptor;
import com.bestvike.stone.spring.shiro.aop.AuthorizingMethodInterceptor;
import lombok.extern.apachecommons.CommonsLog;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;

/**
 * Shiro 注解拦截器
 */
@CommonsLog
@Aspect
@Component
@Order(Ordered.HIGHEST_PRECEDENCE + 1)
public final class ShiroInterceptor {
    private final AuthorizingMethodInterceptor interceptor = new AnnotationsAuthorizingMethodInterceptor();

    @Pointcut("@annotation(com.bestvike.stone.spring.shiro.annotation.RequiresRoles)" +
            " || @annotation(com.bestvike.stone.spring.shiro.annotation.RequiresPermissions)" +
            " || @annotation(com.bestvike.stone.spring.shiro.annotation.RequiresUser)" +
            " || @annotation(com.bestvike.stone.spring.shiro.annotation.RequiresGuest)")
    void shiroPointcut() {
    }

    @Before("shiroPointcut()")
    public void beforeShiroPointcut(JoinPoint joinPoint) {
        this.interceptor.invoke(joinPoint);
    }
}
